﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Budget
{
    public static class Dates
    {
        public static int getWeeksInMonth(int month, int year)
        {
            DateTime dt = getFirstFridayInMonth(month, year);
            int weeks_in_month = 0;
            while (true)
            {
                if (dt.Month == month)
                {
                    weeks_in_month++;
                    dt = dt.AddDays(7);
                }
                else
                {
                    break;
                }
            }
            return weeks_in_month;
        }
        public static int GetDayOfMonth(DateTime _date)
        {
            DateTime firstDay = getFirstTransactionDayOfMonth(_date);
                //getFirstFridayInMonth(_date.Month, _date.Year).AddDays(-6);
            int weeks = getWeeksInMonth(getFridayAssociatedWithDate(firstDay).Month, _date.Year);
            TimeSpan ts = _date - firstDay;
            return ts.Days+1;
        }
        public static int getFridayOfMonthIndex(DateTime _date)
        {
            DateTime temp = getFridayAssociatedWithDate(_date);
            int i = 0;
            foreach (DateTime dt in getFridaysInMonthList(temp.Month, temp.Year))
            {
                if(temp.DayOfYear == dt.DayOfYear)
                {
                    return i;
                }
                i++;
            }
            //shouldn't happen!
            throw new Exception("Error In getFridayOfMonthIndex, no value found");
            return -1;
        }
        public static List<DateTime> getFridaysInMonthList(int month, int year)
        {
            List<DateTime> list_FridaysInMonth = new List<DateTime>();
            DateTime dt = getFirstFridayInMonth(month, year);
            DateTime dt2 = dt;
            while (true)
            {
                if (dt2.Month == month)
                {
                    list_FridaysInMonth.Add(dt2);
                    dt2 = dt2.AddDays(7);
                }
                else
                {
                    break;
                }
            }
            return list_FridaysInMonth;
        }
        public static DateTime getFirstFridayInMonth(int month, int year)
        {
            DateTime dt = new DateTime(year, month, 1);
            while (true)
            {
                if (dt.ToString("dddd") == "Friday")
                {
                    break;
                }
                else
                {
                    dt = dt.AddDays(1);
                }
            }
            return dt;
        }
        public static DateTime getFridayAssociatedWithDate(DateTime _date)
        {
            while (true)
            {
                if (_date.ToString("dddd") == "Friday")
                {
                    break;
                }
                else
                {
                    _date = _date.AddDays(1);
                }
            }
            return _date;
        }
        public static DateTime getFirstTransactionDayOfMonth(DateTime _date)
        {
            DateTime retVal;
            retVal = getFirstFridayInMonth(getFridayAssociatedWithDate(_date).Month, getFridayAssociatedWithDate(_date).Year).AddDays(-6);
            //retVal = getFirstFridayInMonth(_date.Month, _date.Year).AddDays(-6);
            return retVal;
        }
        public static DateTime getLastTransactionDayofMonth(DateTime _date)
        {
            DateTime retVal;
            retVal = getFirstFridayInMonth(getFridayAssociatedWithDate(_date).Month, getFridayAssociatedWithDate(_date).Year);
            retVal = retVal.AddMonths(1);
            retVal = getFirstFridayInMonth(retVal.Month, retVal.Year).AddDays(-7);
            return retVal;
        }
        public static DateTime StringToDateTime(string _date)
        {
            int month, day, year;
            string[] split = _date.Split('/', '-');
            return new DateTime(Convert.ToInt32(split[2].Substring(0,4)), Convert.ToInt32(split[0]), Convert.ToInt32(split[1]));
        }
        public enum MonthName
        {
            January,
            February,
            March,
            April,
            May,
            June,
            July,
            August,
            September,
            October,
            November,
            December
        }

        public static int Month_StringToInt(string Input)
        {
            return ((int)Enum.Parse(typeof(MonthName), Input, true) + 1);
        }
    }
}
